//
//  XJAfterSaleFooterView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/19.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJAfterSaleFooterView.h"
#import "XJAfterSalePhotItem.h"
#import "XJLabelTextField.h"

//默认100 最高200
#define kXJAfterSaleFooterViewClv_h 100
@interface XJAfterSaleFooterView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic, strong) UICollectionView *clv;

// 联系人 练习淡化 提价按钮 view的背景view
@property(nonatomic, strong) UIView *div2;
@property(nonatomic, strong) UIButton *tiJiaoButton; // 提交申请按钮。


@end

@implementation XJAfterSaleFooterView
#pragma mark - 一 lazy
//默认h：100 根据图片个数计算高度。
- (UICollectionView *)clv {
    if (!_clv) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _clv = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KWidth, 100) collectionViewLayout:layout];
        _clv.delegate = self;
        _clv.dataSource = self;
        _clv.backgroundColor = [UIColor whiteColor];
        [_clv registerNib:[UINib nibWithNibName:@"XJAfterSalePhotItem" bundle:nil] forCellWithReuseIdentifier:@"XJAfterSalePhotItem"];
//        _clv.layer.borderColor = [UIColor blackColor].CGColor;
//        _clv.layer.borderWidth = 1.0;
        
        _clv.scrollEnabled = NO; //不让滚动
    }
    return _clv;
}

- (NSMutableArray *)imageArray {
    if (!_imageArray) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

- (UIView *)div2 {
    if (!_div2) {
        _div2 = [UIView new];
    }
    return _div2;
}
- (XJLabelTextField *)nameTextField {
    if (!_nameTextField) {
        _nameTextField = [[XJLabelTextField alloc] initWithFrame:CGRectMake(0, 0, KWidth, 50) keyType:(UIKeyboardTypeDefault) textEntry:NO leftTitle:@"联系人" placeholder:@"请输入"];
    }
    return _nameTextField;
}
- (XJLabelTextField *)phoneTextField {
    if (!_phoneTextField) {
        _phoneTextField = [[XJLabelTextField alloc] initWithFrame:CGRectMake(0, 0, KWidth, 50) keyType:(UIKeyboardTypeDefault) textEntry:NO leftTitle:@"联系人电话" placeholder:@"请输入"];
    }
    return _phoneTextField;
}

- (UIButton *)tiJiaoButton {
    if (!_tiJiaoButton) {
        _tiJiaoButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _tiJiaoButton.backgroundColor = XJCOLOR_RGB(255, 218, 67);
        [_tiJiaoButton setTitle:@"提交申请" forState:(UIControlStateNormal)];
        [_tiJiaoButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        _tiJiaoButton.layer.masksToBounds = YES;
        _tiJiaoButton.layer.cornerRadius = 15.0;
        
        [_tiJiaoButton setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateHighlighted)];
        [_tiJiaoButton addTarget:self action:@selector(tijiaoButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _tiJiaoButton;
}



#pragma mark - 二 init
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    
    [self addSubview:self.clv];
    
    [self addSubview:self.div2];
    [self.div2 addSubview:self.nameTextField];
    [self.div2 addSubview:self.phoneTextField];
    [self.div2 addSubview:self.tiJiaoButton];
}

#pragma mark - 三 layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    
    CGFloat k15 = 15.0;
    
    [self.clv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(self.mas_left).offset(k15);
        make.right.equalTo(self.mas_right).offset(-k15);
        make.height.mas_equalTo(kXJAfterSaleFooterViewClv_h);
    }];
    [self.div2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.clv.mas_bottom);
        make.bottom.equalTo(self);
        make.left.equalTo(self.mas_left).offset(k15);
        make.right.equalTo(self.mas_right).offset(-k15);
    }];
    CGFloat kTF_h = 50;
    
    [self.nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.div2);
        make.top.equalTo(self.div2.mas_top);
        make.height.mas_equalTo(kTF_h);
    }];
    [self.phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.div2);
        make.top.equalTo(self.nameTextField.mas_bottom);
        make.height.mas_equalTo(kTF_h);
    }];
    
    [self.tiJiaoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.div2.mas_centerX);
        make.bottom.equalTo(self.mas_bottom).offset(-30);
        make.height.mas_equalTo(50);
        make.left.right.equalTo(self.div2);
    }];
}


#pragma mark - 四 UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageArray.count + 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XJAfterSalePhotItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"XJAfterSalePhotItem" forIndexPath:indexPath];
    
    if (indexPath.row == self.imageArray.count) {
        
        item.profileIv.image = [UIImage imageNamed:@"plus.png"];
        item.cancelButton.hidden = YES;
    }else if((indexPath.row == self.imageArray.count) && self.imageArray.count > 6){ //从0开始 选择了6张最后一个+号按钮就不显示了。 ❌
        //游戏规则:1.最后一个 (indexPath.row == self.imageArray.count)
        // 最多6个
        // 这个条件永远不满足 ❌
//        item.profileIv.hidden = YES; // 怎么都有bug
        item.hidden = YES;
    }else {
        item.profileIv.image = self.imageArray[indexPath.row];
        item.cancelButton.hidden = NO;
        
        item.cancelPhoneBlock = ^{
            //删除数组中的元素。然后刷新数据
            [self.imageArray removeObjectAtIndex:indexPath.row];
            [self afterSaleReloadData];
        };
    }
    
    
    return item;
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat w = (KWidth - 64)/4;
    return CGSizeMake(w, w);
}

#pragma mark -  UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.imageArray.count) {
        //添加新图片
        if (self.addImageBlock) {
            self.addImageBlock();
        }
    }
}

#pragma mark - D点击事件
- (void)tijiaoButtonAction {

    if (self.clickSubmitButtonBlock) {
        self.clickSubmitButtonBlock();
    }
}



#pragma mark - 外部接口
// 刷新数据。
- (void)afterSaleReloadData {
    [self.clv reloadData];
    [self updateTheHeightOfCollectionView];
}



#pragma mark - Private method
- (void)updateTheHeightOfCollectionView {
    
    if (self.imageArray.count > 3) {//0,1,2,3
        self.clv.rb_height = 200;
    }else{
        self.clv.rb_height = 100;
    }
}

@end
